#include  	"main.h"
#include 	"system.h"
#include	"BSP_LoRa.h"
CollectedData	collectData;
UINT8	Equipment_OnLine_Flag=FAIL;
const char firmwareInf[3][20]={{FW_VERSION},{__DATE__},{__TIME__}};
Sys_Para			SysPara;
struct NetworkDecice_operations NetworkDecice;

int main(void)
{
	uint8	result=0,i=0,Recebuffer[100]={0};
	HAL_Init();
	SystemClock_Config();
	ALLGPIO_Initial();
	Print_UART_Init();
	printf("\n%s-%s-%s\n",firmwareInf[0],firmwareInf[1],firmwareInf[2]);
	printf("HAL_RCC_GetPCLK1Freq=%10d\n",HAL_RCC_GetPCLK1Freq());
	AdcTriggerTimeInitial();
	SysAdcInitial();
	Commu_UART_Init();
	gLCD_ReceData->processed=1;
	WatchdogInitial();
	printf("Data1=%d\n",VREFINT_CAL);
	memcpy((uint8*)&SysPara,(uint8*)InterFlashAddress,sizeof(Sys_Para));
	if(SysPara.thresholdVal_UsedDefault==Not_UsedDefaultValue)
	{
		printf("--->>>����ֵ\nC=%d ",SysPara.thresholdVal.CurThreshold_a);
		printf("%d ",SysPara.thresholdVal.CurThreshold_b);	
		printf("%d ",SysPara.thresholdVal.CurThreshold_c);	
		printf("%d\n",SysPara.thresholdVal.CurThreshold_r); 		
		
		printf("V=%d ",SysPara.thresholdVal.Vol_OverThreshold_a);
		  printf("%d ",SysPara.thresholdVal.Vol_OverThreshold_b);	
		 printf("%d\n",SysPara.thresholdVal.Vol_OverThreshold_c);	
		
		  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_a);
		  printf("%d ",SysPara.thresholdVal.Vol_BelowThreshold_b);
		 printf("%d\n",SysPara.thresholdVal.Vol_BelowThreshold_c);
		
		printf("T=%d ",SysPara.thresholdVal.TemThreshold_a);
		printf("%d ",SysPara.thresholdVal.TemThreshold_b);
		printf("%d ",SysPara.thresholdVal.TemThreshold_c);
		printf("%d\n",SysPara.thresholdVal.TemThreshold_s);	

	}
	else
	{
		printf("--->>>Thershold Use Default Value\n");
		SysPara.thresholdVal.TemThreshold_a=50;	
		SysPara.thresholdVal.TemThreshold_b=50;	
		SysPara.thresholdVal.TemThreshold_c=50;	
		SysPara.thresholdVal.TemThreshold_s=50;	

		SysPara.thresholdVal.Vol_OverThreshold_a=250;
		SysPara.thresholdVal.Vol_OverThreshold_b=250;
		SysPara.thresholdVal.Vol_OverThreshold_c=250;

		SysPara.thresholdVal.Vol_BelowThreshold_a=200;
		SysPara.thresholdVal.Vol_BelowThreshold_b=200;
		SysPara.thresholdVal.Vol_BelowThreshold_c=200;

		SysPara.thresholdVal.CurThreshold_a=80;
		SysPara.thresholdVal.CurThreshold_b=80;
		SysPara.thresholdVal.CurThreshold_c=80;
		
		SysPara.thresholdVal.CurThreshold_r=5000;
		
	}

	if(SysPara.ChannleStatus_UsedDefault==Not_UsedDefaultValue)
	{

		printf("--->>>ͨ������\nC=%02x ",SysPara.ChannleStatus.CurChannle_a);
		printf("%02x ",SysPara.ChannleStatus.CurChannle_b);
		printf("%02x ",SysPara.ChannleStatus.CurChannle_c);
		printf("%02x\n",SysPara.ChannleStatus.CurChannle_r);

		printf("V=%02x ",SysPara.ChannleStatus.VolChannle_a);
		printf("%02x ",SysPara.ChannleStatus.VolChannle_b);
		printf("%02x\n",SysPara.ChannleStatus.VolChannle_c);

		printf("T=%02x ",SysPara.ChannleStatus.TemChannle_a);
		printf("%02x ",SysPara.ChannleStatus.TemChannle_b);
		printf("%02x ",SysPara.ChannleStatus.TemChannle_c);
		printf("%02x\n",SysPara.ChannleStatus.TemChannle_s);



	}
	else
	{
		printf("--->>>ChannleEnalbe_Use Default Value\n");
		SysPara.ChannleStatus.CurChannle_a=Channle_Status_Open;
		SysPara.ChannleStatus.CurChannle_b=Channle_Status_Open;
		SysPara.ChannleStatus.CurChannle_c=Channle_Status_Open;
		SysPara.ChannleStatus.CurChannle_r=Channle_Status_Open;

		SysPara.ChannleStatus.VolChannle_a=Channle_Status_Open;
		SysPara.ChannleStatus.VolChannle_b=Channle_Status_Open;
		SysPara.ChannleStatus.VolChannle_c=Channle_Status_Open;

		SysPara.ChannleStatus.TemChannle_a=Channle_Status_Open;
		SysPara.ChannleStatus.TemChannle_b=Channle_Status_Open;
		SysPara.ChannleStatus.TemChannle_c=Channle_Status_Open;
		SysPara.ChannleStatus.TemChannle_s=Channle_Status_Open;	

	}


	SysPara.sendDataToServerFlag=0;
	SysPara.sendDataTimes=0;

#if 1
	SysPara.ModuleType=CheckBoardModuleType();
	if(SysPara.ModuleType==NBModule)
	{

		NetworkDecice.Init		= BC26Initial;
		NetworkDecice.JoinNet	= BC26_JoinNet;
		NetworkDecice.SendDada	= SendMessageToServer;
	}
	else
	{
		NetworkDecice.Init		= LoRaInit;
		NetworkDecice.JoinNet	= LoRaJoinNet;
		NetworkDecice.SendDada	= LoRaSendDada;
	}
#endif	
	
	NetworkDecice.Init();
	SysCnt=0;
	collectData.GetDeviceID=0;
	Equipment_OnLine_Flag=FAIL;
	SysPara.sendDataToServerFlag=1;
	while (1)
	{

		if(Equipment_OnLine_Flag==FAIL)
		{
			Equipment_OnLine_Flag= NetworkDecice.JoinNet();			
		}

		
		if(SysCnt>1800000)
		{
			
			SysCnt=0;
	
			if(Equipment_OnLine_Flag>=0)
			{
				result=NetworkDecice.SendDada(&collectData);
				if(result==SuccessFull)
				{
					Equipment_OnLine_Flag=FAIL;
				}
			}
		}
		
		if(SysPara.sendDataToServerFlag==1)
		{
			if(Equipment_OnLine_Flag==FAIL)
			{
				Equipment_OnLine_Flag=NetworkDecice.JoinNet();				
			}	
			if(Equipment_OnLine_Flag==SuccessFull)
			{
				result=NetworkDecice.SendDada(&collectData);
				if(result==ERROR)
				{
					Equipment_OnLine_Flag=FAIL;
				}

			}

			SysPara.sendDataToServerFlag=0;
		}

		if(AdcDone_Time==Max_Adc_Time)
		{	
			AdcDone_Time=0;
			Output_AdcVal();
			if(Equipment_OnLine_Flag==SuccessFull)
			{
				collectData.OnLineFlag=1;
			}
			else
			{
				collectData.OnLineFlag=0;
			}
			SendCollectedValToDisplayBoard(CurrentValue,&collectData);
			HAL_Delay(3000);
			SendCollectedValToDisplayBoard(VoltageValue,&collectData);
			HAL_Delay(3000);
			SendCollectedValToDisplayBoard(TemperatureValue,&collectData);
			HAL_Delay(3000);
			if((collectData.GetDeviceID==1)&&((++i%10)==0))
			{
				i=0;
				if(SysPara.ModuleType==NBModule)
				{
					SendCollectedValToDisplayBoard(IMEIValue,&collectData);
				}
				else
				{
					SendCollectedValToDisplayBoard(DEVEUI,&collectData);

				}
			}

			if(Equipment_OnLine_Flag==SuccessFull)
			{
				
				if(ReceiveCmdFromServer(Recebuffer)==ErrCode_ReceData)
				{
					CmdPorcess(Recebuffer);
				}
			}	

			if(SysPara.sendThresholdValueToLcd==1)
			{
				SendCollectedValToDisplayBoard(ThresholdValue,&collectData);
			}
			if(SysPara.sendChannleEnableValueToLcd==1)
			{
				SendCollectedValToDisplayBoard(ChannleEnableValue,&collectData);
			}
			StartAdc();
		}

	}	
}


void Error_Handler(void)
{
	printf("--->>Error\n");
}
